package client.components;

import static client.utils.TBUtils.DEFAULT_DATE_FORMAT;

import java.util.Date;
import java.util.Map;

import client.TimeBookController;
import client.bean.Member;
import client.bean.Plan;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.Format;

public class NewMemberDialogBox extends DialogBox {
	private Plan plan;
	private TextBox name;
	private TextBox email;
	private PlanView planView;

	public NewMemberDialogBox(PlanView planView, Plan plan, Member member) {
		this.planView = planView;
		this.plan = plan;

		VerticalPanel verticalPanel = new VerticalPanel();

		VerticalPanel formPanel = new VerticalPanel();

		name = new TextBox();
		name.setText("John Silver");
		name.addStyleName("thePlanMemberName");
		HorizontalPanel row1 = new HorizontalPanel();
		row1.add(new Label("Name"));
		row1.add(name);
		formPanel.add(row1);

		email = new TextBox();
		email.setText("forvao@gmail.com");
		email.addStyleName("thePlanMemberEmail");

		HorizontalPanel row2 = new HorizontalPanel();
		row2.add(new Label("Email"));
		row2.add(email);
		formPanel.add(row2);

		if (member != null) {
			name.setText(member.getName());
			email.setText(member.getEmail());
			for (Map.Entry<Date, Date> confirmedDates : member.getConfirmedDates().entrySet()) {
				Format format = new com.google.gwt.user.datepicker.client.DateBox.DefaultFormat(DEFAULT_DATE_FORMAT);
				formPanel.add(new Label(">> from: "));
				formPanel.add(new DateBox(null, confirmedDates.getKey(), format));
				formPanel.add(new Label(" to: "));
				formPanel.add(new DateBox(null, confirmedDates.getValue(), format));
				formPanel.add(new Label("Add new date:"));
			}
			email.setText(member.getEmail());
		}

		formPanel.add(new HTML("<hr>"));
		verticalPanel.add(formPanel);

		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(createOkButton());
		buttonPanel.add(createCancelButton());
		verticalPanel.add(buttonPanel);
		add(verticalPanel);
	}

	private Widget createOkButton() {
		Button button = new Button("Ok");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Member member = new Member(name.getValue(), email.getValue());
				TimeBookController.addMember(planView, plan, member);
				hide();
			}
		});
		return button;
	}

	private Widget createCancelButton() {
		Button button = new Button("Cancel");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		return button;
	}

}
